<%@ page pageEncoding="UTF-8"%>
<div id="add_powr_win" class="easyui-dialog"
	style="width: 440px; height: 510px; padding: 10px 20px" closed="true"
	buttons="#dlg-buttons">
	<ul id="powerTree" class="easyui-tree" data-options="animate:true,checkbox:true"></ul>
</div>

<div id="dlg-buttons">
	<a href="javascript:void(0)" class="easyui-linkbutton"
		iconCls="icon-ok" id="save" >保存</a>
	<a href="javascript:void(0)" class="easyui-linkbutton"
		iconCls="icon-cancel"
		onclick="javascript:$('#add_powr_win').dialog('close')">取消</a>
</div>

<script type="text/javascript">
       $('#save').click(function (){
       		var menuIds = getParentNodes('menu');
       		var buttonIds= getParentNodes('button')
       		var nodes = $('#powerTree').tree('getChecked');
			for(var i=0; i<nodes.length; i++){
				var id = nodes[i].id;
				if(id.indexOf('#')>-1){
					if (buttonIds != '') buttonIds += ',';
					buttonIds += id;
				}else{
					if (menuIds != '') menuIds += ',';
					menuIds += id;
				}
			}
			
			//alert('menuIds'+menuIds);
			//alert('buttonIds'+buttonIds);
			if(menuIds==''){
				$.messager.alert('角色设置权限','请选择需要设置的用户权限', 'info');
			}
			
			$.post('system/role/setRolePower.action',{menuCodes:menuIds,funCodes:buttonIds,roleCode:selectRole}, function (data){
               $.messager.alert('角色设置权限', data.message, 'info');
            });
			            
    	});
    	
    	//得到树半选中状态的父节点
    	function getParentNodes(type){
    		 var ids = '';
    		 $("#powerTree span").each(function(){
    			var _stateClass = $(this).attr("class");
    			if('tree-checkbox tree-checkbox2'==_stateClass){
    				var id = $(this).parent().attr("node-id");
    				if(type=='button'){
    					if(id.indexOf('#')>-1){
							if (ids != '') ids += ',';
							ids += id;
						}
    				}else{
    					if (ids != '') ids += ',';
							ids += id;
    				}
    			}
        	});
        	
        	return ids;
    	}
  		
		$('#add_user_form').keypress(function(event){
			if (event.keyCode!=13){
 				return;
			}
			saveUser();
		});
</script>
